package indi.zhang.logs.test;

import indi.zhang.logs.test.util.Journal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.web.servlet.function.EntityResponse.fromObject;

@Component
public class GreetingHandler {

    Journal journal;

    @Autowired
    public GreetingHandler(Journal journal) {
        this.journal = journal;
    }

    private static final org.slf4j.Logger Logger =
            org.slf4j.LoggerFactory.getLogger(GreetingHandler.class);

    @GetMapping("/greeting")
    ServerResponse greeting(ServerRequest request) {
        journal.log("Message sent to journal DB");
        Logger.error("Something else is wrong here");
        return ServerResponse.ok().contentType(TEXT_PLAIN).body(fromObject("Hello World!"));
    }
}
